package com.zhdl.hardware.plc.siemens.exc;

import com.google.common.eventbus.Subscribe;
import com.project.command.faq.event.behavior.BehaviorPerform;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.hardware.plc.siemens.entity.SiemensReceiveData;
import com.zhdl.hardware.plc.siemens.repository.SiemensCmdHandleDao;
import com.zhdl.modules.tools.CommonCallback;
import com.zhdl.network.duplexprotocol.DuplexProtocolSendService;
import com.zhdl.network.strategy.common.ProtocolContext;
import com.zhdl.network.strategy.modbustcp.ModbusTcpStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class SiemensReceiveHandelDaoImpl implements SiemensCmdHandleDao {

    /**
     * PLC西门子-网口
     */
    public String port;
    /**
     * 通信协议处理
     */
    public ProtocolContext protocolContext;
    /**
     * Callback 回调管理
     */
    public Map<String, CommonCallback<String>> commonCallbackMap = new ConcurrentHashMap<>();

    public SiemensReceiveHandelDaoImpl(String port, BehaviorPerform behaviorPerform) {
        EventBusCenter.getEventBus().register(this);
        protocolContext = new ProtocolContext(new ModbusTcpStrategy(port, behaviorPerform));
        this.port = port;
    }

    String data_read = "", data_write = "";

    @Subscribe
    public void dataReception(SiemensReceiveData event) {
        String dataAll = event.getData_all();
        String dataBefore = dataAll.substring(0,120);
        String dataAfter = dataAll.substring(120,240);
        data_read = dataBefore;
        data_write = dataAfter;
    }
    
    public void raedValueByAddress(String slaveId, String address, int count, CommonCallback<String> callback) {
        int startAddress = Integer.parseInt(address, 16);
        int startNum = startAddress * 4;
        int endNum = startNum + (count * 4);
        String hexData;
        if ("0".equals(slaveId)){
            hexData = data_read.substring(startNum, endNum);
        } else {
            hexData = data_write.substring(startNum, endNum);
        }
        callback.statusCallback(hexData);
    }
    
    public void writeValueByAddress(String slaveId, String address, String value) {
        int decAddress = Integer.parseInt(address,16);
        int startNum = decAddress * 4;
        StringBuilder stringBuffer = new StringBuilder(data_write);
        stringBuffer.replace(startNum,startNum+4,value);
        DuplexProtocolSendService.sendCmd(stringBuffer.toString());
    }

    
    public void writeValuesByAddress(String slaveId, String address, String value) {
    }

    /**
     * 读取plc的值
     *
     * @param address  寄存器地址
     * @param count    寄存器个数
     * @param callback 回调
     */
    
    public void raedValueByAddress(String address, int count, CommonCallback<String> callback) {
    }

    /**
     * 写入plc的值
     *
     * @param address 寄存器地址
     * @param value   寄存器值
     */
    
    public void writeValueByAddress(String address, String value) {

    }

    
    public void writeValuesByAddress(String address, String value) {

    }
}
